package com.com.designpatterns.visitor;

/**
 * Created by pankaj on 7/12/15.
 */
public abstract class Tree2<E> {

    interface Visitor<E, R>{
        public <R> R leaf(E elt);
        public <R> R branch (R r, R l);
    }

    public abstract <R> R visit(Visitor<E,R> visitor);

    public static <T> Tree2<T> leaf(T elt){
        return new Tree2<T>(){
            @Override
            public <R> R visit(Visitor<T, R> visitor) {
                return visitor.leaf(elt);
            }
        };
    }

    public static <T> Tree2<T> branch(Tree2<T> l, Tree2<T> r){
        return new Tree2<T>(){
            @Override
            public <R> R visit(Visitor<T, R> visitor) {
                return visitor.branch(l.visit(visitor), r.visit(visitor));
            }
        };
    }




}


